import utils from '@/utils/util'

export function pidEchartSeries({
  name = '',
  data,
  labelPosition = 'outside',
  radius = [0, '75%']
}) {
  let label = {
    show: true,
    position: labelPosition,
    fontSize: utils.pxToFixPx(14, 'number', 12),
    color: '#313131',
    formatter: "{b}\n{num|{c} ({d}%)}",
    rich: {
      num: {
        fontSize: 13,
        color: '#999',
        lineHeight: 20
      }
    }
  };
  return {
    name,
    type: 'pie',
    avoidLabelOverlap: false,
    labelLine: {
      show: true
    },
    data,
    label,
    radius
  }
}

export function barEchartSeries({
  name = '',
  data,
  labelPosition = 'inside',
  stack = '',
  barGap = 0
}) {
  let label = {
    show: true,
    position: labelPosition,
    textStyle: {
      fontSize: 12,
    },
    formatter: function (params) {
      return params.value
    }
  };
  return {
    name,
    type: 'bar',
    stack,
    barMaxWidth: '100', // 柱状图最大宽度
    data,
    barGap,
    label
  }
}

export function lineEchartSeries({
  name = '',
  data,
  stack = '',
}) {
  // let label = {
  //   show: true,
  //   textStyle: {
  //     fontSize: 12,
  //   },
  //   formatter: function (params) {
  //     return params.value
  //   }
  // };
  return {
    name,
    type: 'line',
    data,
    stack
  }
}

/**
 * 系统运行监控统计报表，过滤柱状图中数据为0的柱状图
 * @param {*} counts 接口返回的柱状图数据
 * @param {*} keys 对应的柱状图数据的字段（图例字段）
 */
export function filterStatisticZero(counts, keys) {
  let result = []

  for (let i = 0; i < counts.length; i++) {
    const count = counts[i]
    for (let j = 0; j < keys.length; j++) {
      const key = keys[j]
      if (count[key]) {
        result.push(count)
        break
      }
    }
  }
  return result
}
